<?php


namespace app\unify\controller;


use app\BaseController;
use app\unify\model\UserLog as UserLogModel;
use app\unify\model\User as UserModel;

use think\db\Query;
use think\Paginator;
use think\Request;

class UserLog extends BaseController
{
    /**
     * 用户日志列表
     * @param \think\Request $r
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function index(Request $r): Paginator
    {
        $where = [];

        $keywords = $r->param('keywords');
        if ($keywords) {

            $userIds = UserModel::where([
                ['username', 'LIKE', "%{$keywords}%"]
            ])->column("id");
            $userIds[] = -1;


            $where[] = function (Query $q) use ($keywords, $userIds) {
                $q->whereOr([
                    ['user_id', 'in', $userIds],
                    ['url|description', 'LIKE', "%{$keywords}%"]
                ]);
            };
        }

        return UserLogModel::where($where)->withoutField(['response', 'request'])->append(['userName'])->order("id DESC")->paginate($this->limit);

    }
}